package com.yuezhu.util;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTimeCountDown {
	private int recLen = 11;
	private long hours;
	private long minutes;
	private long seconds;
	private String minString;
	private String secondString;
	private TextView txtView;
	private long diff;
	private long days;
	private long millTime;
	private int id;
	private Context context;
private TimeTo timeTo ;
private boolean canDown = true ;
	public DateTimeCountDown(TextView txtView, Context context, TimeTo timeTo) {
		this.txtView = txtView;
		this.context = context;
		this.timeTo = timeTo ;
		// getTime();

	}
public void  setCanDown(boolean down){
	canDown = down ;
}
	public void begin(long millTime, int id) {
		this.id = id;
		this.millTime = millTime;
		getTime();
		Message message = handler.obtainMessage(1); // Message
		handler.sendMessageDelayed(message, 1000);
	}
public void  stopTime(){
	handler.removeMessages(1);
}
	final Handler handler = new Handler() {

		public void handleMessage(Message msg) { // handle message
			switch (msg.what) {
			case 1:
				diff = diff - 1000;
				if (!canDown) {
					return ;
				}
				getShowTime();
				if (diff > 0) {
					Message message = handler.obtainMessage(1);
					handler.sendMessageDelayed(message, 1000);
				} else {
					if (txtView!=null) {
						txtView.setText("不能抢单");
					}
				
					timeTo.noSignUp();
				}
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	/**
	 * 得到时间差
	 */
	private void getTime() {

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String date = sDateFormat.format(new Date());
		try {
			/*System.out
					.println("现在时间23232："
							+ sDateFormat.format(System.currentTimeMillis() + 60000L * 140));*/
			Date d2 = df.parse(date);
			Date d1 = df.parse(sDateFormat.format(millTime));
			// Date d1 = df.parse("2018-02-22 12:45:33");
			diff = d1.getTime() - d2.getTime();// 这样得到的差值是微秒级别
			days = diff / (1000 * 60 * 60 * 24);
			hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
			minutes = (diff - days * (1000 * 60 * 60 * 24) - hours
					* (1000 * 60 * 60))
					/ (1000 * 60);
			seconds = (diff - days * (1000 * 60 * 60 * 24) - hours
					* (1000 * 60 * 60) - minutes * (1000 * 60)) / (1000);
			// txtView.setText(""+days+"天"+hours+"小时"+minutes+"分"+seconds+"秒");

			int m = 54;
			WriteToTextView();
		} catch (Exception e) {
		}
	}

	/**
	 * 获得要显示的时间
	 */
	private void getShowTime() {
		days = diff / (1000 * 60 * 60 * 24);
		hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
		minutes = (diff - days * (1000 * 60 * 60 * 24) - hours
				* (1000 * 60 * 60))
				/ (1000 * 60);
		seconds = (diff - days * (1000 * 60 * 60 * 24) - hours
				* (1000 * 60 * 60) - minutes * (1000 * 60)) / (1000);
		WriteToTextView();
	}

	/**
 * 
 */
	private void WriteToTextView() {
		if (Integer.parseInt(minutes + "") < 10) {
			minString = "0" + minutes;
		} else {
			minString = "" + minutes;
		}
		if (Integer.parseInt(seconds + "") < 10) {
			secondString = "0" + seconds;
		} else {
			secondString = "" + seconds;
		}
		if (txtView!=null) {
			txtView.setText(minString + ":" + secondString);
		}
		
	}
	public interface TimeTo{
		void noSignUp();
	}
}
